package com.cugmarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cugmarket.entity.ProductVoucher;
import com.cugmarket.service.ProductVoucherService;
import com.cugmarket.mapper.ProductVoucherMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.cugmarket.constant.MessageConstant.INSERT_ERROR;
import static com.cugmarket.constant.RedisConstants.SECKILL_STOCK_KEY;

/**
* @author duoch
* @description 针对表【product_voucher】的数据库操作Service实现
* @createDate 2024-11-04 20:27:59
*/
@Service
public class ProductVoucherServiceImpl extends ServiceImpl<ProductVoucherMapper, ProductVoucher> implements ProductVoucherService{
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void add(ProductVoucher productVoucher) {
        if (!save(productVoucher)) {
           throw new RuntimeException(INSERT_ERROR);
        }
        stringRedisTemplate.opsForValue().set(SECKILL_STOCK_KEY + productVoucher.getId(), productVoucher.getStock().toString());
    }
}




